package com.zhangxin.aiInterview.manager.disruptor.strategy;

import cn.hutool.json.JSONUtil;
import com.zhangxin.aiInterview.manager.work_flow.tts_boy.TTsBoy;
import com.zhangxin.aiInterview.manager.work_flow.tts_girl.TTsGirl;
import com.zhangxin.aiInterview.manager.disruptor.WsEvent;
import com.zhangxin.aiInterview.manager.wsserver.WsManager;
import com.zhangxin.aiInterview.manager.wsserver.enums.WsMessageTypeEnum;
import com.zhangxin.aiInterview.manager.wsserver.model.WsResponseMessage;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;

/**
 * 通过智能体将文本转换为语音
 */
@Service
public class TTsByAgentSendMsg implements WsSendMsg{
    @Override
    public void sendMessage(WsEvent wsEvent) {
        try {
            String txt = wsEvent.getTxt().trim();
            Long userId = wsEvent.getUserId();
            String gender = wsEvent.getGender();
            //音频网络链接
            String audioUrl;
            System.out.println("文本转语音");
            if("男".equals(gender)){
                audioUrl = TTsBoy.generateAudio(txt);
            }else {
                audioUrl = TTsGirl.generateAudio(txt);
            }
            System.out.println("ai面试官语音url:"+audioUrl);

            //获取客户端ws连接
            WebSocketSession wsSession = WsManager.getConnection(userId);
            WsResponseMessage wsResponseMessage = new WsResponseMessage(WsMessageTypeEnum.AUDIO.getValue(), audioUrl);
            //返回音频访问路径返回给客户端
            wsSession.sendMessage(new TextMessage(JSONUtil.toJsonStr(wsResponseMessage)));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
